package com.starry.module.system.core.oauth2.authorization.grant;

import com.starry.core.common.domain.R;
import com.starry.module.system.core.oauth2.authorization.utils.SecurityUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import java.io.IOException;

/**
 * @Description 自定义授权错误响应处理器
 * @Author xiaoke
 */
public class CustomGrantErrorResponseHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        SecurityUtils.writeResponse(R.fail(HttpStatus.BAD_REQUEST.value(), exception.getMessage()), response);
    }
}
